package Thread;

    public class Test3 {
        static class TicketSeller implements Runnable {
            private int tickets = 1000;
            @Override
            public void run() {
                while (true) {
                    synchronized (this) {
                        if (tickets > 0) {
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + " 售出了第 " + tickets + " 张票");
                            tickets--;
                        } else {
                            break;
                        }
                    }
                }
            }
        }

        public static void main(String[] args) {
            TicketSeller ticketSeller = new TicketSeller();
            Thread window1 = new Thread(ticketSeller, "窗口1");
            Thread window2 = new Thread(ticketSeller, "窗口2");
            Thread window3 = new Thread(ticketSeller, "窗口3");
            Thread window4 = new Thread(ticketSeller, "窗口4");
            window1.start();
            window2.start();
            window3.start();
            window4.start();
        }
    }

